package si.yun.web.handler;

import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * @ClassName LoginHandler
 * @Author SXM
 * @Date 2021/07/28 15:01
 * @Version 1.0
 */
public class LoginHandler implements HandlerInterceptor {
    private ObjectMapper objectmapper=new ObjectMapper();

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
            throws Exception {
        //判断登录请求
        String requestURI = request.getRequestURI();
        if(requestURI.indexOf("/login")!=-1) {
            return true;
        }
        //判断登录状态
        HttpSession session = request.getSession();
        Object obj=session.getAttribute("user");
        //System.out.println(obj);
        if(obj!=null) {
            return true;
        }
        //非登录情况重新登录
        response.sendRedirect("/login/SyLogin.html");
        return false;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,
                           ModelAndView modelAndView) throws Exception {
        // TODO Auto-generated method stub
    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
            throws Exception {
        // TODO Auto-generated method stub
    }
}
